<!--
 * @Descripttion: 
 * @Author: zhanyi
 * @Date: 2021-10-25 21:09:56
 * @FilePath: \wczd-mobile\src\pages\component\scan-box\scan-box.vue
-->
<template>
  <div class="scan-box">
    <dragButton :existTabBar="true" :isDock="true" @btnClick='_btnClick'></dragButton>
  </div>
</template>

<script>
import dragButton from "@/components/drag-button/drag-button";
import { getInspectionDetailByUniqueCode } from "@/api/task";
var qrcode = require("@/utils/reqrcode.js")

export default {
  name: 'scan-box',
  components: { dragButton },
  data () {
    return {}
  },
  methods: {
    _btnClick () {
      if (navigator) {
        let that = this
        uni.chooseImage({
          sizeType: ['original'],
          count: 1,
          success: function (res) {
            let tempFilePaths = res.tempFilePaths
            qrcode.decode(tempFilePaths[0])
            qrcode.callback = function (img) {
              if (img == "error decoding QR Code") {
                uni.showToast({
                  title: "识别二维码失败，请重新上传！",
                  duration: 2000,
                  icon: 'none'
                })
              } else {
                getInspectionDetailByUniqueCode({ objUniqueCode: img }).then((res) => {
					if(res.inspectionItemLst.length>0){
						uni.navigateTo({
							url: '/pages/task/task-edit?nodeId=' + (res.inspectionRoadNode ? res.inspectionRoadNode.nodeId : '') + '&taskId=' + (res.inspectionRoadNode ? res.inspectionRoadNode.taskId : '') + '&objUniqueCode=' + img
						})
					}
					else{
						uni.showToast({
						  title: "此二维码不是风险码",
						  duration: 2000,
						  icon: 'none'
						})
					}
                })
              }
            }
          }
        })
      } else {
        uni.scanCode({
          success: data => {
            // 扫到之后的处理
            getInspectionDetailByUniqueCode({ objUniqueCode: data.result }).then((res) => {
              uni.navigateTo({
                url: '/pages/task/task-edit?nodeId=' + (res.inspectionRoadNode ? res.inspectionRoadNode.nodeId : '') + '&taskId=' + (res.inspectionRoadNode ? res.inspectionRoadNode.taskId : '') + '&objUniqueCode=' + data.result
              })
            })
          },
          fail: err => {
            // 需要注意的是小程序扫码不需要申请相机权限
          }
        });
      }
      // var data = {
      //  result: '0f46cc7b4fc3482ab5dfde1cec2d8a47'
      // }
      // getInspectionDetailByUniqueCode({objUniqueCode: data.result}).then((res)=>{
      // 	uni.navigateTo({
      // 	  url: '/pages/task/task-edit?nodeId='+(res.inspectionRoadNode?res.inspectionRoadNode.nodeId:'')+'&taskId='+(res.inspectionRoadNode?res.inspectionRoadNode.taskId:'')+'&objUniqueCode='+data.result
      // 	})
      // })
    }
  },
}
</script>

<style scoped lang="scss">
.scan-box {
}
</style>
